package audio;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;

public class AudioSystem {
	private Logger logger = Logger.getLogger(AudioSystem.class.getName());

	private Map<String,Sound> soundMap = new HashMap<String,Sound>();
	public void loadSoundsFromFolder(File soundFolder){
		for(File soundFile : soundFolder.listFiles()){
			if(soundFile.getName().endsWith(".ogg")||soundFile.getName().endsWith(".wav")){
				String pathName = soundFile.getPath().replace("\\","/");
				boolean success = loadSound(pathName);
				if(!success){				
					logger.warning("could not load sound: "+pathName);
				}
			}
		}
		System.out.println("sounds: "+soundMap);
	}
	
	public boolean playMusic(){
		return false;
	}

	public boolean loadSound(String url){
		try{
			Sound sound = Gdx.audio.newSound(Gdx.files.internal(url));
			soundMap.put(url,sound);
		}catch(Exception e){
			logger.severe(e.getLocalizedMessage());
			return false;
		}
		return true;
	}
	public long playSound(String url, float volume,boolean loop){
		long soundId = -1;
		Sound sound = soundMap.get(url);
		if(sound==null){
			logger.warning("no sound: "+url);
			return soundId;
		}		
		try{
			if(loop){
				soundId = sound.loop(volume);
			}else{
				soundId = sound.play(volume);
			}
		}catch(UnsatisfiedLinkError e){
			logger.warning("sound system closed");
		}
		return soundId;

	}
	public boolean stopSound(String url,long loopId){
		Sound sound = soundMap.get(url);
		if(sound==null){
			return false;
		}	
		sound.stop(loopId);
		return true;
	}

}
